package com.lovo.sqlatm;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Withdrawal extends JPanel implements ActionListener {
    private Test0 frame;
    private JTextField teller =new JTextField();


    private Icon conImage = Util.getIcon("/img/buttonSure.jpg");
    private Icon cancelImage = Util.getIcon("/img/buttonOff.jpg");
    private JButton confirmButton;
    private JButton cancelButton;

    public Withdrawal(Test0 frame) {
        this.frame=frame;
        this.setLayout(null);

        JLabel tellerText = new JLabel("请输入你要取款的金额：");
        tellerText.setBounds(314, 338, 160, 40);
        this.add(tellerText);
        //输入取款金额
        teller.setBounds(454,338,150,40);
        teller.getText();
        this.add(teller);

        //确认按钮
        confirmButton=new JButton(conImage);
        confirmButton.setBounds(354,400,140,50);
        confirmButton.addActionListener(this);
        this.add(confirmButton);
        //返回按钮
        cancelButton=new JButton(cancelImage);
        cancelButton.setBounds(540,400,140,50);
        this.add(cancelButton);
        cancelButton.addActionListener(this);

        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==confirmButton){
            int tellerMoney = Integer.parseInt(teller.getText());

            if (tellerMoney<=frame.getUesr().getBalance()) {
                int money = frame.getUesr().getBalance() - tellerMoney;
                frame.getUesr().setBalance(money);
                frame.getUesr().setBalance(frame.getUesr().getBalance());
                String account =frame.getUesr().getAccount();
                String password =frame.getUesr().getPassword();
                int balan =frame.getUesr().getBalance();
                Test01.update(account,password,balan);
                JOptionPane.showMessageDialog(this,frame.getUesr().getBalance());

                frame.switchPanel("menu");
            }else {
                JOptionPane.showMessageDialog(this,"你的金额不足，请重新输入");
            }

        }else if (e.getSource()==cancelButton){
            frame.switchPanel("menu");
        }

    }
}
